<?php

/**
 * This is the model class for table "websites".
 *
 * The followings are the available columns in table 'websites':
 * @property integer $id
 * @property string $name
 * @property string $alias
 * @property integer $id_categoiresWebsite
 * @property string $description
 * @property string $content
 * @property string $image
 * @property string $pageDemo
 * @property string $md5PageDemo
 * @property string $type
 * @property integer $size
 * @property integer $created
 * @property integer $updated
 * @property integer $numberBuy
 * @property integer $numberRegister
 */
class Websites extends PIActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'websites';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, alias, id_categoiresWebsite, description, content', 'required', "message"=>"{attribute}  không được trống"),
			//array('id_categoiresWebsite, size, created, updated, numberBuy, numberRegister', 'numerical', 'integerOnly'=>true),
			//array('name, alias, image, pageDemo, md5PageDemo', 'length', 'max'=>255),
			array('description', 'length', 'max'=>500),
			array('type', 'length', 'max'=>30),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, alias, id_categoiresWebsite, description, content, image, pageDemo, md5PageDemo, type, size, created, updated, numberBuy, numberRegister', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'categories' => array(self::BELONGS_TO, 'CategoriesWebsite', 'id_categoiresWebsite'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Tên website',
			'alias' => 'Alias',
			'id_categoiresWebsite' => 'Danh mục website',
			'description' => 'Mô tả',
			'content' => 'Nội dung',
			'image' => 'Hình ảnh đại diện',
			'pageDemo' => 'Trang demo',
			'md5PageDemo' => 'Md5 Page Demo',
			'type' => 'Kiểu',
			'size' => 'Kích thước',
			'created' => 'Ngày đăng',
			'updated' => 'Ngày cập nhập',
			'numberBuy' => 'Số người mua',
			'numberRegister' => 'Số người đăng ký',
			'price' => 'Giá mua phần mềm',
			'priceFull' => 'Giá mua trộn bộ',
			'imageDetail' => 'Hình ảnh chi tiết',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('t.name',$this->name,true);
		//$criteria->compare('alias',$this->alias,true);
		$criteria->compare('id_categoiresWebsite',$this->id_categoiresWebsite);
		//$criteria->compare('description',$this->description,true);
		//$criteria->compare('content',$this->content,true);
		//$criteria->compare('image',$this->image,true);
		//$criteria->compare('pageDemo',$this->pageDemo,true);
		//$criteria->compare('md5PageDemo',$this->md5PageDemo,true);
		////$criteria->compare('type',$this->type,true);
		//$criteria->compare('size',$this->size);
		//$criteria->compare('created',$this->created);
		//$criteria->compare('updated',$this->updated);
		//$criteria->compare('numberBuy',$this->numberBuy);
		//$criteria->compare('numberRegister',$this->numberRegister);
		$criteria->with = array('categories');
		$criteria->together = true;

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getDataWebsite()
	{
		$dataProvider=new CActiveDataProvider('Websites', array('criteria'=>array('select'=>'id, name')));
		$arr = $dataProvider->getData();
		$data_Categories = array();
		$data_Categories[] = '-- Chọn theme --';
		foreach($arr as $v){
			$data_Categories[$v->id] = $v->name;
		}
		return $data_Categories;
	}
	
	public function getDataWebsite1()
	{
		$dataProvider=new CActiveDataProvider('Websites', array('criteria'=>array('select'=>'id, name')));
		$arr = $dataProvider->getData();
		$data_Categories = array();
		$data_Categories[] = '-- Chọn theme --';
		$data_Categories[""] = '-- Hiển thị tất cả --';
		foreach($arr as $v){
			$data_Categories[$v->id] = $v->name;
		}
		return $data_Categories;
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Websites the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
